package com.zzq.controller;

import com.zzq.context.BaseContext;
import com.zzq.pojo.Admin;
import com.zzq.pojo.Result;
import com.zzq.pojo.vo.DatebaseAnalyze;
import com.zzq.service.AdminService;
import com.zzq.service.FileService;
import com.zzq.tools.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/admin")
@Validated
public class AdminController {

    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    public Result<String> login(@Pattern(regexp = "^[a-zA-Z0-9_]{3,20}$") String username, @Pattern(regexp = "^[a-zA-Z0-9_]{6,20}$") String password){
        Admin admin=adminService.findByAdminName(username);
        if(admin!=null){
            if(admin.getPassword().equals(password)){
                Map<String, Object> claims=new HashMap<>();
                claims.put("id",admin.getId());
                claims.put("username",admin.getUsername());
                String token= JwtUtil.getToken(claims);
                return Result.success(token);
            }else {
                return Result.error(500,"密码错误");
            }
        }else{
            return Result.error(500,"用户名错误");
            }
    }

    @PostMapping("/logout")
    public Result<String> logout() {

        BaseContext.removeCurrentId();

        log.info(BaseContext.getCurrentId());

        return Result.success();
    }

    @GetMapping("/user")
    public Result<Admin> getUser(String username){
        Admin admin=adminService.findByAdminName(username);
        return Result.success(admin);
    }

    @PatchMapping("/updatePwd")
    public Result<String> updatePassword(@RequestBody Map<String,String> map){
        String oldPwd=map.get("oldPwd");
        String newPwd=map.get("newPwd");
        String username=map.get("username");
        if(!StringUtils.hasLength(oldPwd)||!StringUtils.hasLength(newPwd)){
            return Result.error(500,"密码不能为空");
        }
        Admin admin=adminService.findByAdminName(username);
        if(!admin.getPassword().equals(oldPwd)){
            return Result.error(500,"旧密码错误");
        }

        adminService.updatePassword(username,newPwd);
        return Result.success();
    }


    @PostMapping("/uploadFile")
    public Result uploadFile(MultipartFile file) throws IllegalStateException, IOException {

        String originFileName = file.getOriginalFilename();
        file.transferTo(new File("D:\\uploadfiles\\"+originFileName));
        return Result.success("url");
    }

    @GetMapping("/datebase")
    public Result<DatebaseAnalyze> getDatebase(){
        return Result.success(adminService.getDatebase());

    }
}
